/*
 * @(#)QuickToolBar.java   09/09/13
 *
 * Copyright (C) 2009 Ecole des Mines d'Ales
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Contact: yulinz88@gmail.com
 *
 *
 *
 */



package fr.ema.arch3d.gea.mainGUI;

import fr.ema.arch3d.gea.Evolution;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JToolBar;

/**
 * Class description
 *
 *
 * @version        1.0, 09/09/13
 * @author         Huaxi (Yulin) ZHANG
 */
public class QuickToolBar extends JPanel implements ActionListener {

    /** Field description */
    static final private String ADD = "add";

    /** Field description */
    static final private String DELETE = "delete";

    /** Field description */
    static final private String REPLACE = "replace";

    /** Field description */
    protected String newline = "\n";

    /** Field description */
    protected JTextArea textArea;

    /**
     * Constructs ...
     *
     */
    public QuickToolBar() {
        super(new BorderLayout());

        // Create the toolbar.
        JToolBar toolBar = new JToolBar("Still draggable");

        addButtons(toolBar);

        // Create the text area used for output.  Request
        // enough space for 5 rows and 30 columns.
        // textArea = new JTextArea(5, 30);
        // textArea.setEditable(false);
        // JScrollPane scrollPane = new JScrollPane(textArea);
        // Lay out the main panel.
        // setPreferredSize(new Dimension(450, 130));
        add(toolBar, BorderLayout.PAGE_START);

        // add(scrollPane, BorderLayout.CENTER);
    }

    protected void addButtons(JToolBar toolBar) {
        JButton button = null;

        // first button
        button = makeNavigationButton("add", ADD, "Add a component", "Add");
        toolBar.add(button);

        // second button
        button = makeNavigationButton("delete", DELETE, "Delete a component", "Delete");
        toolBar.add(button);

        // third button
        button = makeNavigationButton("replace", REPLACE, "Replace a component", "Replace");
        toolBar.add(button);
    }

    protected JButton makeNavigationButton(String imageName, String actionCommand, String toolTipText, String altText) {

        // Look for the image.
        String imgLocation = "resource/icons/" + imageName + ".png";
        URL    imageURL    = Evolution.class.getResource(imgLocation);

        // Create and initialize the button.
        JButton button = new JButton();

        button.setActionCommand(actionCommand);
        button.setToolTipText(toolTipText);
        button.addActionListener(this);

        if (imageURL != null) {    // image found
            button.setIcon(new ImageIcon(imageURL, altText));
        } else {                   // no image found
            button.setText(altText);
            System.err.println("Resource not found: " + imgLocation);
        }

        return button;
    }

    public void actionPerformed(ActionEvent e) {
        String cmd         = e.getActionCommand();
        String description = null;

        // Handle each button.
        if (ADD.equals(cmd)) {               // first button clicked
            description = "taken you to the previous <something>.";
        } else if (DELETE.equals(cmd)) {     // second button clicked
            description = "taken you up one level to <something>.";
        } else if (REPLACE.equals(cmd)) {    // third button clicked
            description = "taken you to the next <something>.";
        }

        displayResult("If this were a real app, it would have " + description);
    }

    protected void displayResult(String actionDescription) {
        textArea.append(actionDescription + newline);
        textArea.setCaretPosition(textArea.getDocument().getLength());
    }
}
